/* 
 * Enterprise Library Extensions - DI + ORM.
 * Copyright (C) 2006
 *
 * GNU General Public License
 *
 * This program is free software; you can redistribute 
 * it and/or modify it under the terms of the GNU General Public License 
 * as published by the Free Software Foundation; either version 2 of the 
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 * See the GNU General Public License for more details.
 */
namespace ELE.Common.Util
{

	using System;
	using System.Collections;
  using System.Text.RegularExpressions;
  using System.Text;
  using System.Globalization;
	/// <summary> A utility class for manipulating dates.
	/// 
	/// </summary>
	/// <version>  	1.0 27 Jun 2003
	/// </version>
	/// <author>  	Miguel Curi
	/// 
	/// </author>
	public class DateUtils
	{
    /// <summary>
    /// Calculate current age based on a birth date
    /// </summary>
    /// <param name="pDob">date of birth</param>
    /// <returns>age in years</returns>
    public static int CalculateAge(DateTime pDob)
    {
      return CalculateAge(pDob, DateTime.Now);
    }

    /// <summary>
    /// Calculate current age based on a birth date and supplied date
    /// </summary>
    /// <param name="pDob">date of birth</param>
    /// <param name="pFromDate">date to calculate age from</param>
    /// <returns>age in years</returns>
    public static int CalculateAge(DateTime pDob, DateTime pFromDate)
    {
      int age = pFromDate.Year - pDob.Year;

      if(pFromDate.Month <= pDob.Month && pFromDate.Day < pDob.Day)
        age--;

      return age;
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="dateStyle"></param>
    /// <param name="timeStyle"></param>
    /// <param name="culture"></param>
    /// <returns></returns>
    public static DateTimeFormatInfo GetDateTimeFormatInstance(int dateStyle, int timeStyle, CultureInfo culture)
    {
      DateTimeFormatInfo format = culture.DateTimeFormat;

      switch (timeStyle)
      {
        case -1:
          DateTimeFormatManager.manager.SetTimeFormatPattern(format, "");
          break;

        case 0:
          DateTimeFormatManager.manager.SetTimeFormatPattern(format, "h:mm:ss 'o clock' tt zzz");
          break;

        case 1:
          DateTimeFormatManager.manager.SetTimeFormatPattern(format, "h:mm:ss tt zzz");
          break;

        case 2:
          DateTimeFormatManager.manager.SetTimeFormatPattern(format, "h:mm:ss tt");
          break;

        case 3:
          DateTimeFormatManager.manager.SetTimeFormatPattern(format, "h:mm tt");
          break;
      }

      switch (dateStyle)
      {
        case -1:
          DateTimeFormatManager.manager.SetDateFormatPattern(format, "");
          break;

        case 0:
          DateTimeFormatManager.manager.SetDateFormatPattern(format, "dddd, MMMM dd%, yyy");
          break;

        case 1:
          DateTimeFormatManager.manager.SetDateFormatPattern(format, "MMMM dd%, yyy");
          break;

        case 2:
          DateTimeFormatManager.manager.SetDateFormatPattern(format, "d-MMM-yy");
          break;

        case 3:
          DateTimeFormatManager.manager.SetDateFormatPattern(format, "M/dd/yy");
          break;
      }

      return format;
    }

  }
}